package com.wen.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wen.common.R;
import com.wen.pojo.ProductType;
import com.wen.pojo.dto.ProductAndProductType;
import com.wen.pojo.dto.ProductDto;
import com.wen.pojo.dto.ProductTypeDto;
import com.wen.service.ProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/ProductType")
public class ProductTypeController {


    @Autowired
    private ProductTypeService productTypeService;


//    @GetMapping("/list")
//    public R list() {
//        List<ProductAndProductType> list = productTypeService.getselectProductORProductType();
//        return R.ok().data("product",list);
//    }


    @GetMapping("/type")
    public R categoryHead(){
        List<ProductType> productTypeList = productTypeService.list();
        return R.ok().data("children",productTypeList);
    }






    //------------------------后台-----------------------------

    /**
     * 商品类型管理
     * 带条件查询
     * @param dto
     * @return
     */
    @PostMapping("/productTypePage/{pageNum}/{pageSize}")
    public R page(@RequestBody ProductTypeDto dto,
                  @PathVariable Integer pageNum,
                  @PathVariable Integer pageSize){
        Page<ProductType> medicinesPage = productTypeService.queryPage(dto,pageNum,pageSize);
        return R.ok().data("records",medicinesPage.getRecords())
                .data("total",medicinesPage.getTotal())
                .data("current",medicinesPage.getCurrent())
                .data("size",medicinesPage.getSize());

    }


    @PostMapping("/productTypequeryPage/{pageNum}/{pageSize}")
    public R querypage(@RequestBody ProductDto dto,
                  @PathVariable Integer pageNum,
                  @PathVariable Integer pageSize){
        System.out.println(dto);
        Page<ProductAndProductType> medicinesPage = productTypeService.queryProductAndProductTypePage(dto,pageNum,pageSize);
        return R.ok().data("records",medicinesPage.getRecords())
                .data("total",medicinesPage.getTotal())
                .data("current",medicinesPage.getCurrent())
                .data("size",medicinesPage.getSize());

    }


    @PostMapping("/productTypequeryPageTwo/{pageNum}/{pageSize}")
    public R querypageTwo(@RequestBody ProductDto dto,
                       @PathVariable Integer pageNum,
                       @PathVariable Integer pageSize){
        System.out.println(dto);
        Page<ProductAndProductType> medicinesPage = productTypeService.getSelectAdvance(dto,pageNum,pageSize);
        return R.ok().data("records",medicinesPage.getRecords())
                .data("total",medicinesPage.getTotal())
                .data("current",medicinesPage.getCurrent())
                .data("size",medicinesPage.getSize());

    }



    /**
     * 商品类型管理
     * 修改
     * @param productType
     * @return
     */
    @PutMapping("/productTypeUpdate")
    public  R updateProductType(@RequestBody ProductType productType){
        boolean flag = productTypeService.updateById(productType);
        if (flag) {
            return R.ok().message("修改成功！");
        }
        return R.error().message("修改失败！");

    }

    /**
     * 商品类型管理
     * 删除
     * @param typeId
     * @return
     */
    @DeleteMapping("/productTypeDelete/{typeId}")
    public R removeProductType(@PathVariable Long typeId){
//        QueryWrapper<ProductType> wrapper = new QueryWrapper<>();
//        wrapper.eq("type_id",id);
//        boolean flag = productTypeService.remove(wrapper);
        boolean flag = productTypeService.removeById(typeId);

        if (flag) {
            return R.ok().message("删除成功！");
        }
        return R.error().message("删除失败！");

    }

    /**
     * 商品类型管理
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/productTypeGetId/{id}")
    public R getById(@PathVariable String id){
//        ProductType selectById = productTypeService.getById(id);

        System.out.println(id);
        HashMap<String, Object> map = new HashMap<>();

        map.put("id",id);

        List<ProductType> productTypes = productTypeService.listByMap(map);

        return R.ok().data("selectById",productTypes);
    }


    /**
     * 商品类型管理
     * 添加
     * @param productType
     * @return
     */
    @PostMapping("/ProductTypeAdd")
    public R addProductType(@RequestBody ProductType productType){
        int flag = productTypeService.addProductType(productType);
        if (flag>0) {
            return R.ok().message("添加成功！");
        }
        return R.error().message("添加失败！");
    }


    /**
     * 一级分类查询
     * @return
     */
    @GetMapping("/selLevelOne")
    public R selLevelOne(){
        List<ProductType> productTypes = productTypeService.levelOne();
        return R.ok().data("productTypes",productTypes);
    }

    /**
     * 二级分类查询
     * @return
     */
    @GetMapping("/selLevelTwo")
    public R selLevelTwo(){
        List<ProductType> productTypes = productTypeService.levelTwo();
        return R.ok().data("productTypes",productTypes);
    }


    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PostMapping("/del")
    public boolean del(@RequestBody List<String> ids) {
        return productTypeService.removeByIds(ids);
    }


}
